package com.lestar.www.wuliancamtest.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lestar.www.wuliancamtest.R;
import com.lestar.www.wuliancamtest.WulianCam.Device;
import com.lestar.www.wuliancamtest.WulianCam.ISipListener;
import com.lestar.www.wuliancamtest.WulianCam.WulianCamManager;
import com.wulian.siplibrary.api.SipController;

import org.webrtc.videoengine.ViERenderer;

import rx.Subscriber;

/**
 * User: EEE
 * Date: 2016/8/3
 * Time: 16:45
 */
public class DeviceDetailActivity extends Activity implements ISipListener, View.OnClickListener {

    private Device mDevice;
    private SurfaceView cameraPreview;
    ViewGroup previewFrame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_detail);
        mDevice = (Device) getIntent().getSerializableExtra(DeviceListActivity.DEVICE_KEY);
        previewFrame = (ViewGroup) findViewById(R.id.preview_frame_rl);

        loadCamSubscription();

        previewFrame.setOnClickListener(this);
        findViewById(R.id.unbind_btn).setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        WulianCamManager.getInstance(this).uninstallSip();
        super.onDestroy();
    }

    @Override
    public void onSipCallChanged(int callId) {
        if (callId >= 0) {
            if (cameraPreview.getVisibility() == View.GONE) {
                cameraPreview.setVisibility(View.VISIBLE);
            }
            SipController.getInstance().setVideoAndroidRenderer(callId, cameraPreview);
        }
    }

    @Override
    public void onSipAlarmReceived(String alarm) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.preview_frame_rl:
                loadCamSubscription();
                break;
            case R.id.unbind_btn:
                WulianCamManager.getInstance(this).unbind(mDevice.getDid())
                        .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(DeviceDetailActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onNext(String s) {
                        Toast.makeText(DeviceDetailActivity.this, s, Toast.LENGTH_LONG).show();
                    }
                });

                break;
            default:
                break;
        }
    }

    private void loadCamSubscription() {
        WulianCamManager.getInstance(this).loadCamPreview(this, mDevice, this)
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        if (cameraPreview == null) {
                            cameraPreview = ViERenderer.CreateRenderer(DeviceDetailActivity.this, true, false); // cb_pictureReverse.isChecked()
                            cameraPreview.setVisibility(View.VISIBLE);
                            previewFrame.addView(cameraPreview);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(DeviceDetailActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onNext(String s) {
                        Toast.makeText(DeviceDetailActivity.this, s, Toast.LENGTH_LONG).show();
                    }
                });
    }
}
